package org.darkforest.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.darkforest.common.wxpay.utils.HttpTool;
import org.darkforest.model.RestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "lottery")
@Api(value = "lottery", description = "彩虹包", produces = MediaType.APPLICATION_JSON_VALUE)
public class LotteryController {

    Logger logger = LoggerFactory.getLogger(LotteryController.class);

    // 测试环境
    public final static String token_url = "https://testel.bayss.cn/baysslottery/interfaceAuthority/authorityToken.htmls?appId=APPID&appSecret=SECRET";
    public final static String lottery_url = "https://testel.bayss.cn/baysslottery/redLotteryHouse/requestRedLottery.htmls?appId=APPID&tokenId=TOKENID&activeId=ACTIVEID&customerId=CUSTOMERID";
    public final static String day_record_url = "https://testel.bayss.cn/baysslottery/redLotteryHouse/getRedLotteryDayRecord.htmls?appId=APPID&tokenId=TOKENID&activeId=ACTIVEID";

    // 生产环境
//    public final static String token_url = "https://easylink.bayss.cn/baysslottery/interfaceAuthority/authorityToken.htmls?appId=APPID&appSecret=SECRET";
//    public final static String lottery_url = "https://easylink.bayss.cn/baysslottery/redLotteryHouse/requestRedLottery.htmls?appId=APPID&tokenId=TOKENID&activeId=ACTIVEID&customerId=CUSTOMERID";
//    public final static String day_record_url = "https://easylink.bayss.cn/baysslottery/redLotteryHouse/getRedLotteryDayRecord.htmls?appId=APPID&tokenId=TOKENID&activeId=ACTIVEID";

    /**
     * 获取易连互动公众号的TokenID
     *
     * @return
     */
    @RequestMapping(value = "getTokenId", method = RequestMethod.GET)
    @ApiOperation(value = "TokenID的取得", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getTokenId(@RequestParam(required = true) String appId, @RequestParam(required = true) String appSecret) {
        try {
            // String getTokenUrl = "https://testel.bayss.cn/baysslottery/interfaceAuthority/authorityToken.htmls?appId=APPID&appSecret=SECRET";
            String requestUrl = token_url.replace("APPID", appId).replace("SECRET", appSecret);

            // 返回  {"msg":"SUCCESS","res":{"tokenId":"3648672a3d625c84c46aec6965d6246013952f9976a7634bec46c7b04c44fb6eb5f7b5134121305d6138413575327e75"},"code":200}
            JSONObject jsonObject = HttpTool.httpRequest(requestUrl, "GET", null);
            logger.info("getTokenId="+jsonObject.toString());

            Map<String,String> result = new HashMap<String,String>();
            String  msg = jsonObject.getString("msg");
            String  code = jsonObject.getString("code");

            result.put("code", code);
            result.put("msg", msg);

            if ("200".equals(code)) {
                String  res = jsonObject.getString("res");

                JSONObject resJsonObject = JSONObject.fromObject(res);
                String  tokenId = resJsonObject.getString("tokenId");

                result.put("res", res);
                result.put("tokenId", tokenId);
            }

            return RestEntity.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("获取异常");
        }
    }

    /**
     * 获取易连互动公众号的彩红包
     *    领取一个彩虹包
     *
     * @return
     */
    @RequestMapping(value = "getRedLottery", method = RequestMethod.GET)
    @ApiOperation(value = "领取一个彩虹包", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getRedLottery(@RequestParam(required = true) String appId, @RequestParam(required = true) String tokenId,
                                    @RequestParam(required = true) String activeId, @RequestParam(required = true) String customerId) {
        try {
            //String getTokenUrl = "https://testel.bayss.cn/baysslottery/redLotteryHouse/requestRedLottery.htmls?appId=APPID&tokenId=TOKENID&activeId=ACTIVEID&customerId=CUSTOMERID";
            String requestUrl = lottery_url.replace("APPID", appId).replace("TOKENID", tokenId).replace("ACTIVEID", activeId).replace("CUSTOMERID", customerId);

            // 返回  {"msg":"SUCCESS","res":{},"code":200}
            JSONObject jsonObject = HttpTool.httpRequest(requestUrl, "GET", null);
            logger.info("getRedLottery="+jsonObject.toString());

            Map<String,String> result = new HashMap<String,String>();
            String  msg = jsonObject.getString("msg");
            String  code = jsonObject.getString("code");

            result.put("code", code);
            result.put("msg", msg);

            // 根据数据集code判断，如果是200，就调用彩红包明细接口

            return RestEntity.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("获取异常");
        }
    }

    /**
     * 获取易连互动公众号的彩红包
     *    获取当天彩红包的领取个数
     *
     * @return
     */
    @RequestMapping(value = "getRedLotteryCount", method = RequestMethod.GET)
    @ApiOperation(value = "获取当天彩红包的领取个数", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getRedLotteryCount(@RequestParam(required = true) String appId, @RequestParam(required = true) String tokenId,
                                         @RequestParam(required = true) String activeId) {
        try {
            //String getTokenUrl = "https://testel.bayss.cn/baysslottery/redLotteryHouse/getRedLotteryDayRecord.htmls?appId=APPID&tokenId=TOKENID&activeId=ACTIVEID";
            String requestUrl = day_record_url.replace("APPID", appId).replace("TOKENID", tokenId).replace("ACTIVEID", activeId);

            // 返回  {"code":200,"recordList":[{"receiveTime":1520579541000,"customerId":"111111"}],"recordNum":1}
            JSONObject jsonObject = HttpTool.httpRequest(requestUrl, "GET", null);
            logger.info("getRedLotteryCount="+jsonObject.toString());

            Map<String, Object> result = new HashMap<String, Object>();
            String  code = jsonObject.getString("code");
            result.put("code", code);

            // 根据数据集code判断，如果是200，返回结果列表
            if ("200".equals(code)) {
                String  recordNum = jsonObject.getString("recordNum");
                result.put("recordNum", recordNum);

                // 结果列表
                List recordList = (List)jsonObject.get("recordList");
                // 根据 customerId取得砸中金蛋的用户名
                System.out.println("recordList.size()="+recordList.size());

                result.put("recordList", recordList);
            } else {
                result.put("recordNum", "0");
            }

            return RestEntity.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed("获取异常");
        }
    }
}